/**
 * Created by Joe on 2017/7/5 0005.
 */
var Partner = {
    user: {
        fields: {
            selects: [], //下拉框
            radios: ["userType"],//单选框
            checkboxs: [],//复选款
            textareas: [],//多行文本框
            selfDefine: [{"field": "metroLine ", "selector": "a.metro_line"}]
        }
    },
    address: {
        selects: ["province", "city", "district"] //下拉框
    }
};
$(document).ready(function () {
    initDatatable();
    /** init baidu uploader*/
    Utils.console_log("type of  Uploader", typeof(Uploader));
    Utils.console_log("typeof(Uploader) == 'undefined'", typeof(Uploader) == 'undefined');
    if (typeof(Uploader) !== 'undefined') {
        Uploader.initUploader(Uploader.server.company_bm_images);
    }
    handlerEvent();
});

function handlerEvent() {
    /*图片上传业务处理*/
    $('#company-detail-form').on('click', '#uploader-bm', function (e) {
        $('#filePicker input[name=file]').click();
        Uploader.target_alert = "company-detail-form";
        Uploader.target_server = Uploader.server.company_bm_images;
    });

    $('#company-detail-form').on('click', '#uploader-am', function (e) {
        $('#filePicker input[name=file]').click();
        Uploader.target_alert = "company-detail-form";
        Uploader.target_server = Uploader.server.company_am_images;
    });

    $('#company-detail-form').on('click', '#uploader-cm', function (e) {
        $('#filePicker input[name=file]').click();
        Uploader.target_alert = "company-detail-form";
        Uploader.target_server = Uploader.server.company_cm_images;
    });
    /*用户业务事件处理*/
    $('#btn-add-user').on('click', function () {
        $("#save-new-user").show();
        $("#save-update-user").hide();
        $("#partner-user-modal").modal("toggle");
    });
    $('#save-new-user').on('click', function () {
        requestSaveUser();
    });
    $('#save-update-user').on('click', function () {
        requestUpdateUser();
    });
    $('#DataTables_partner_user').on('click', ".btn-detail-user", function () {
        $("#save-new-user").hide();
        $("#save-update-user").show();
        requestGetUserDetail($(this).siblings("input").val());
    });
    $('#DataTables_partner_user').on('click', ".btn-delete-user", function () {
        requestRemoveUser($(this));
    });
    /*地址业务事件处理*/
    $('#btn-add-address').on('click', function () {
        $("#save-new-address").show();
        $("#save-update-address").hide();
        $('#save-setDefault').hide();
        $("#partner-address-modal").modal("toggle");
        City.fillOptionWithArr("loc_province", 0, null);
    });
    $('#save-new-address').on('click', function () {
        requestSaveAddress();
    });
    $('#save-update-address').on('click', function () {
        requestUpdateAddress();
    });
    $('#save-setDefault').on('click', function () {
        var id = $("#address-form").find("input[name='id']")[0].val();
        requestSetDefault(id);
    });
    $('#DataTables_partner_address').on('click', ".btn-detail-address", function () {
        $("#save-new-address").hide();
        $("#save-update-address").show();
        $('#save-setDefault').show();
        requestGetAddressDetail($(this).siblings("input").val());
    });
    $('#DataTables_partner_address').on('click', ".btn-remove-address", function () {
        requestRemoveAddress($(this));
    });
    /*发票业务事件处理*/
    $('.btn-detail-invoice').on('click', function () {
        $("#partner-invoice-modal").modal("toggle");
    });
    $(".btn-preview").click(function () {
        var imageUrl = $(this).prev().val();
        Utils.preview(imageUrl);
    });
    $(".btn-save").on("click", function () {
        requestUpdateCompany();
    });
}
function requestUpdateCompany() {
    var url = /*[[@{/partner/updateCompany}]]*/ '/partner/updateCompany',
        params = $("#company-detail-form").serialize();
    Utils.ajaxSubmit(url, params, function (data) {
        Utils.console_log("data ： ", data);
        if (data.result == "success") {
            layer.msg('数据已更新完成', {time: 3000, icon: 6});
        }
        if (data.result == "fail") {
            Utils.errorAlert(data.errorMessage);
        }
    });
}
function requestSaveUser() {
    var url = /*[[@{/customer/createUser}]]*/ '/customer/createUser',
        params = $("#user-form").serialize();
    Utils.ajaxSubmit(url, params, function (data) {
        Utils.console_log("data ： ", data);
        if (data.result == "success") {
            layer.msg('用户数据已保存完成', {time: 3000, icon: 6});
            $("#partner-detail-modal").modal("toggle");
            addTableSingleRow(data.content, '#DataTables_partner_user');
        }
        if (data.result == "fail") {
            Utils.errorAlert(data.errorMessage);
        }
    });
}
function requestUpdateUser() {
    var url = /*[[@{/customer/updateUser}]]*/ '/customer/updateUser',
        params = $("#user-form").serialize();
    Utils.ajaxSubmit(url, params, function (data) {
        Utils.console_log("data ： ", data);
        if (data.result == "success") {
            $("#partner-user-modal").modal("toggle");
            layer.msg('用户账户添加完成', {time: 3000, icon: 6});
        }
        if (data.result == "fail") {
            Utils.errorAlert(data.errorMessage);
        }
    });
}
function requestGetUserDetail(value) {
    $("#user-form").find("input[type!='checkbox'][type!='radio']").val('');
    var url = /*[[@{/customer/loadByUserId}]]*/ '/customer/loadByUserId';
    Utils.ajaxSubmit(url.concat("/").concat(value), {}, function (data) {
        Utils.console_log("data ： ", data);
        if (data.result == "success") {
            Utils.fillForms(data.content, Partner.user.fields);
            $("#partner-user-modal").modal("toggle");
        }
        if (data.result == "fail") {
            Utils.errorAlert(data.errorMessage);
        }
    });
}
function requestRemoveUser(targetEle) {
    var url = /*[[@{/customer/deleteUser}]]*/ '/customer/deleteUser';
    Utils.ajaxSubmit(url + "/" + targetEle.siblings("input").val(), {}, function (data) {
        if (data.result == "success") {
            //delete tr row
            var $tr = targetEle.parents('tr');
            var row = $('#DataTables_partner_user').DataTable().row($tr);
            row.remove().draw();
            layer.msg('备件删除成功', {time: 3000, icon: 6});
        }
        if (data.result == "fail") {
            Utils.errorAlert(data.errorMessage);
        }
    });
}
function addTableSingleRow(v, targetEle) {
    v.userType = v.userType == '0' ? "主账户" : "子账户";
    v.isActive = v.isActive == '1' ? "已激活" : "未激活";
    var btnRemoveEle = '<input type="hidden" value="' + v.id + '"><a class="btn btn-primary btn-detail-user">查看明细</a>';
    btnRemoveEle = btnRemoveEle.concat('&nbsp;&nbsp;&nbsp;<a class="btn btn-danger btn-delete-user">删除</a>');
    $(targetEle).dataTable().fnAddData([v.userName, v.realName, v.job, v.userType, v.isActive, v.createAt, btnRemoveEle]);
}

function requestSaveAddress() {
    var url = /*[[@{/address/createCompanyAddress}]]*/ '/address/createCompanyAddress',
        params = $("#address-form").serialize();
    Utils.ajaxSubmit(url, params, function (data) {
        Utils.console_log("data ： ", data);
        if (data.result == "success") {
            $("#partner-address-modal").modal("toggle");
            layer.msg('收货地址添加完成', {time: 3000, icon: 6});
            addTableAddressSingleRow(data.content, '#DataTables_partner_address');
        }
        if (data.result == "fail") {
            Utils.errorAlert(data.errorMessage);
        }
    });
}
function requestUpdateAddress() {
    var url = /*[[@{/address/updateCompanyAddress}]]*/ '/address/updateCompanyAddress',
        params = $("#address-form").serialize();
    Utils.ajaxSubmit(url, params, function (data) {
        Utils.console_log("data ： ", data);
        if (data.result == "success") {
            $("#partner-address-modal").modal("toggle");
            layer.msg('数据已更新完成', {time: 3000, icon: 6});
        }
        if (data.result == "fail") {
            Utils.errorAlert(data.errorMessage);
        }
    });
}
function requestSetDefault(value) {
    var url = /*[[@{/address/setDefaultAddress}]]*/ '/address/setDefaultAddress';
    Utils.ajaxSubmit(url.concat("/").concat(value), {}, function (data) {
        Utils.console_log("data ： ", data);
        if (data.result == "success") {
            layer.msg('设置已完成', {time: 3000, icon: 6});
        }
        if (data.result == "fail") {
            Utils.errorAlert(data.errorMessage);
        }
    });
}
function requestGetAddressDetail(value) {
    Utils.clearForm("#address-form");
    var url = /*[[@{/address/getAddressById}]]*/ '/address/getAddressById';
    Utils.ajaxSubmit(url.concat("/").concat(value), {}, function (data) {
        Utils.console_log("data ： ", data);
        if (data.result == "success") {
            Utils.fillForms(data.content, Partner.user.fields);
            $("#partner-address-modal").modal("toggle");
            City.fillOptionWithArr("loc_province", 0, data.content.province);
            City.fillOptionWithArr("loc_city", data.content.province, data.content.city);
            City.fillOptionWithArr("district", data.content.city, data.content.district);
        }
        if (data.result == "fail") {
            Utils.errorAlert(data.errorMessage);
        }
    });
}
function requestRemoveAddress(targetEle) {
    var url = /*[[@{/address/deleteAddressById}]]*/ '/address/deleteAddressById';
    Utils.ajaxSubmit(url + "/" + targetEle.siblings("input").val(), {}, function (data) {
        if (data.result == "success") {
            //delete tr row
            var $tr = targetEle.parents('tr');
            var row = $('#DataTables_partner_address').DataTable().row($tr);
            row.remove().draw();
            layer.msg('备件删除成功', {time: 3000, icon: 6});
        }
        if (data.result == "fail") {
            Utils.errorAlert(data.result.errorMessage);
        }
    });
}
function addTableAddressSingleRow(v, targetEle) {
    var btnRemoveEle = '<input type="hidden" value="' + v.id + '"><a class="btn btn-primary btn-detail-address">查看明细</a>';
    btnRemoveEle = btnRemoveEle.concat('&nbsp;&nbsp;&nbsp;<a class="btn btn-danger btn-remove-address">删除</a>');
    $(targetEle).dataTable().fnAddData([v.companyName, v.preAddress + v.address, v.contactTel, v.contact, v.createAt, btnRemoveEle]);
}

function initDatatable() {
    var url = /*[[@{/vendor/datatables/zh-cn.json}]]*/ '/vendor/datatables/zh-cn.json';
    $('#DataTables_partner_user').DataTable({
        "paging": true,
        "searching": true,
        "info": true,
        "ordering": true,
        "language": {
            "url": url
        }
    });
    /*    $('#DataTables_partner_user tbody').on('click', 'tr', function () {
     var data = user_table.row(this).data();
     });*/
    $('#DataTables_partner_invoice').DataTable({
        "paging": true,
        "searching": true,
        "info": true,
        "ordering": true,
        "language": {
            "url": url
        }
    });
    $('#DataTables_partner_address').DataTable({
        "paging": true,
        "searching": true,
        "info": true,
        "ordering": true,
        "language": {
            "url": url
        }
    });

}